package org.deyang.tools.amazon.pricetracker;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;

import org.deyang.tools.amazon.pojos.PriceChangeEntity;

public class SimplePriceAnalyzer extends PriceAnalyzer {
	private static final Logger log = Logger.getLogger(SimplePriceAnalyzer.class.getName());
	private List<PriceChangeEntity> ePriceHistory;
	private String asinValue;
	
	public SimplePriceAnalyzer(String asin, String priceList){
		this.ePriceHistory = this.generateProcessList(priceList);
		this.asinValue = asin;
	}
	
	
	private boolean checkDealStatus(int daysPeriod){
		Calendar calendar = new GregorianCalendar(Locale.UK);
		Calendar severDaysAgoCalendar = new GregorianCalendar(Locale.UK);
		Calendar sixtyDaysAgoCalendar = new GregorianCalendar(Locale.UK);
		calendar.add(Calendar.HOUR, -(daysPeriod * 24));
		severDaysAgoCalendar.add(Calendar.HOUR, -(daysPeriod * 7));
		sixtyDaysAgoCalendar.add(Calendar.HOUR, -(daysPeriod * 60));
		
		List<PriceChangeEntity> priceHistory = this.ePriceHistory;

		
		if (priceHistory != null && priceHistory.size() > 0){
			boolean isPriceInPeriod = false;
			boolean isPriceTrendDown = false;
			int highPrice = 0;
			int lowPrice = 0;
			int edgePrice = 0;
			
			//initial latest price and current amazon price
			int size = priceHistory.size();
			int lastRecordPriceAmount = priceHistory.get(size - 1).getPriceValue();
			
			
			//Show deal information if it is a free digital product
			if (lastRecordPriceAmount == 0){
				this.dd.setFreeContent(true);
				this.dd.setAmazonDeal(true);
				return true;
			}else if (lastRecordPriceAmount == -1){
				//No availabel price history data
				log.warning("No available latest price data for asin: " + this.asinValue);
				return false;
			}

			if (priceHistory.get(0).getCalendarValue().compareTo(sixtyDaysAgoCalendar) > 0 && priceHistory.get(0).getCalendarValue().compareTo(severDaysAgoCalendar) < 0){
				//if the price data is not enough for 60 days and not a new product within 7days, I will ignore it
				return false;
				
			}
			
			for (int i = 0; i < priceHistory.size(); i++){
				if (!isPriceInPeriod && priceHistory.get(i).getCalendarValue().compareTo(calendar) > 0 && priceHistory.get(i).getPriceValue() > -1){
					//find a price change after edge date
					isPriceInPeriod = true;
					if (i > 0){
						// try to get the price at edge date
						highPrice = priceHistory.get(i - 1).getPriceValue();
						lowPrice = highPrice;
						edgePrice = lowPrice;
					}else{
						//if this is first price record, then take it as edge date price
						highPrice = priceHistory.get(i).getPriceValue();
						lowPrice = highPrice;
						edgePrice = lowPrice;
					}
				}
				if (isPriceInPeriod && priceHistory.get(i).getPriceValue() > -1){
					if (priceHistory.get(i).getPriceValue() > highPrice){
						highPrice = priceHistory.get(i).getPriceValue();
					}else if (priceHistory.get(i).getPriceValue() < lowPrice){
						lowPrice = priceHistory.get(i).getPriceValue();
					}
				}
			}
			
			//Check trend
			if (size > 1 && (priceHistory.get(size - 1).getPriceValue() < priceHistory.get(size - 2).getPriceValue())){
				isPriceTrendDown = true;
			}			
			
			if (isPriceTrendDown 
					&& lastRecordPriceAmount <= lowPrice 
					&& (lowPrice < highPrice * 0.97 || (highPrice - lowPrice) > 300)
					&& lastRecordPriceAmount < edgePrice){

				this.dd.setAmazonDeal(true);
				return true;
			}
			
			
		}else{
			return false;
		}
		return false;
		
	}

	@Override
	public boolean isDeal(int daysPeriod) {
		if (this.ePriceHistory == null){
			//No price information, so return false
			return false;
		}

		return checkDealStatus(daysPeriod);
	}

}
